#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
#include <errno.h>
#include <sys/poll.h>

#define MYPORT 10001
#define MAXLINE 1024

#define MAX_CONNECT 10

//采用select单线程模式


int main(){
    //服务器初始化过程
    int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(listen_fd < 0){
        perror("socket()");
        return 1;
    }
    struct sockaddr_in seraddr;
    memset(&seraddr, 0, sizeof(seraddr));
    seraddr.sin_family = AF_INET;
    seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    seraddr.sin_port = htons(MYPORT);
    if(bind(listen_fd, (struct sockaddr*)&seraddr, sizeof(seraddr)) < 0){
        perror("bind()");
        return 1;
    }
    if(listen(listen_fd,3) < 0){
        perror("listen()");
        return 1;
    }

    //存放客户端地址
    socklen_t cli_len;
    struct sockaddr_in cliaddr;

    //连接用fd 和 测试用fd
    int connfd;
    int testfd;

    int maxi = 0;
    int i;
    int nready;
    struct pollfd client[MAX_CONNECT];
    ssize_t n;
    char buf[MAXLINE];


    client[0].fd = listen_fd;
    client[0].events = POLLIN;
    for(i = 1; i < MAX_CONNECT; i++){
        client[i].fd = -1;
    }

    while(1){
        nready = poll(client, maxi + 1, -1);

        if(nready < 0){
            continue;
        }
        //处理连接套接字
        if(client[0].revents & POLLIN){
            cli_len = sizeof(cliaddr);
            connfd = accept(listen_fd, (struct sockaddr*)&cliaddr, &cli_len);
            char temp[256];
            printf("accept from %s, port %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, temp, cli_len), ntohs(cliaddr.sin_port));
            if(connfd < 0){
                perror("accept()");
                exit(1);
            }
            for(i = 1; i < MAX_CONNECT; i++){
                if(client[i].fd < 0){
                    client[i].fd = connfd;
                    client[i].events = POLLIN;
                    break;
                }
            }
            if(i == MAX_CONNECT){
                close(connfd);
            }
            else{
                if(i > maxi){
                    maxi = i;
                }
            }
            if(--nready <= 0){
                continue;
            }
        }

        //处理读写
        for(i = 1; i <= maxi; i++){
            if((testfd = client[i].fd) < 0){
                continue;
            }
            if(client[i].revents & (POLLIN | POLLERR)){
                if((n = read(testfd, buf, MAXLINE)) < 0){
                    if(errno == ECONNRESET){
                        close(testfd);
                        client[i].fd = -1;
                    }
                    else{
                        perror("read()");
                        close(testfd);
                        client[i].fd = -1;
                    }
                }
                else if(n == 0){
                    close(testfd);
                    client[i].fd = -1;
                }
                else{
                    write(testfd, buf, n);
                }
                if(--nready <= 0){
                    break;
                }
            }
        }
    }
    return 0;
}